/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveCommands
{
	class GrooveDeleteSpace : CommandUtils.GrooveUtilBase
	{
		private string m_SpaceURI;
		
		private bool m_AllDevices;
		private bool m_LastManager;
		private bool m_Shutdown;

		[STAThread]
		static void Main(string[] i_Arguments)
		{
			CommandUtils.UtilAppMain.RunApplication(new GrooveDeleteSpace(i_Arguments));
		}

		public GrooveDeleteSpace(string[] i_Arguments)
			: base (i_Arguments)
		{
			base.UsageMessage = "GrooveDeleteSpace [PARAMS...]";
			base.HelpMessage = 
				"GrooveDeleteSpace\r\n\t" + 
				"Deletes specified Groove space(s)\r\n" +
				"Parameters:\r\n\t" +
				"/Space=              The Web Services URI for the Groove space or * for all\r\n\t" +
				"[/Account=           The Web Services URI for the Groove account]\r\n\t" +
				"[/Identity=          The Web Services URI for the Groove identity]\r\n\t" +
				"[/AllDevices         Delete space on all devices for identity]\r\n\t" +
				"[/LastManager        Delete space even if identity is last manager]\r\n\t" +
				"[/Shutdown           Delete space for all members]\r\n\t" +
				"[/RequestKey=        Token used to communicate Groove host]\r\n\t" +
				"[/Host=              The remote Groove host name to issue commands to]\r\n\t" +
				"[/Port=              The remote Groove host port to issue commands to]\r\n\t" +
				"[/Timeout=           Timeout (in ms) for Web Services method execution]\r\n\t" +
				"[/X=                 Configuration file to use for parameter input]\r\n\t" +
				"\r\n";
		}

		public override CheckResult CheckParameters()
		{
			CommandUtils.UtilBase.CheckResult CheckResult = base.CheckParameters();
			if (CheckResult != CommandUtils.UtilBase.CheckResult.Succeeded )
				return CheckResult;
		
			m_SpaceURI = base.GetParameterValue("Space");
			if (System.String.IsNullOrEmpty(m_SpaceURI))
			{
				base.DisplayUsage("You must specify a space.");
				return CommandUtils.UtilBase.CheckResult.Failed;
			}

			m_AllDevices = base.GetParameterValueFlag("AllDevices");

			m_LastManager = base.GetParameterValueFlag("LastManager");

			m_Shutdown = base.GetParameterValueFlag("Shutdown");

			return base.CheckUnusedParameters();
		}

		public override CommandUtils.UtilBase.CheckResult Run()
		{
			try
			{
				if (m_SpaceURI == "*")
				{
					//  Process all spaces
					GrooveWebServicesV12Helpers.Space[] Spaces = base.Identity.StandardSpaces;

					foreach (GrooveWebServicesV12Helpers.Space Space in Spaces)
					{
						if (m_Shutdown)
						{
							//  Uninvite all members except for current identity
							System.Collections.ArrayList MemberURIArrayList = new System.Collections.ArrayList();

							GrooveWebServicesV12Helpers.Member[] Members = Space.Members;

							foreach (GrooveWebServicesV12Helpers.Member Member in Members)
							{
								if (Member.URI != Space.IdentityURL)
									MemberURIArrayList.Add(Member.URI);
							}

							if (MemberURIArrayList.Count > 0)
							{
								//  Create array from array list
								string[] MemberURIs = (string[])MemberURIArrayList.ToArray(typeof(string));

								Space.Uninvite(MemberURIs);
							}
						}

						//  Delete space for current identity
						base.Identity.DeleteSpace
							(
							Space.URI, 
							m_AllDevices,
							m_LastManager
							);
					}
				}
				else
				{
					//  Process specified space only
					if (m_Shutdown)
					{
						//  Uninvite all members except for current identity
						GrooveWebServicesV12Helpers.Space Space = base.Identity.OpenSpace(m_SpaceURI);;

						System.Collections.ArrayList MemberURIArrayList = new System.Collections.ArrayList();

						GrooveWebServicesV12Helpers.Member[] Members = Space.Members;

						foreach (GrooveWebServicesV12Helpers.Member Member in Members)
						{
							if (Member.URI != Space.IdentityURL)
								MemberURIArrayList.Add(Member.URI);
						}

						if (MemberURIArrayList.Count > 0)
						{
							//  Create array from array list
							string[] MemberURIs = (string[])MemberURIArrayList.ToArray(typeof(string));

							Space.Uninvite(MemberURIs);
						}
					}

					//  Delete space for current identity
					base.Identity.DeleteSpace
						(
						m_SpaceURI, 
						m_AllDevices,
						m_LastManager
						);
				}
			}
			catch (System.Exception Exception)
			{
				base.StandardError.WriteLine("Unable to delete space: " + Exception.Message);
				throw Exception;
			}

			return CommandUtils.UtilBase.CheckResult.Succeeded;
		}
	}

}
